package com.line.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.config.entity.Equipment;
import com.config.entity.LineConfig;
import com.config.service.EquipmentService;
import com.config.service.LineConfigService;
import com.line.service.LineService;

@RequestMapping("/line")
@Controller
public class LineController {
	@Resource(name="lineConfigService")
	private LineConfigService lineConfigService;
	@Resource(name="lineService")
	private LineService lineService;
	@Resource(name="equipmentService")
	private EquipmentService equipmentService;

	/**
	 * 整线电度电费分析
	 * @return
	 */
	@RequestMapping("/electricity/{type}")
	public String electricityAnalysis(Model model,@PathVariable(value = "type") String type){
		model.addAttribute("currentName","整线电度电费分析");
		if(type.equals("day")){//日
			return "line/electricity/electricityday";
		}else if(type.equals("complex")){//日综合
			return "line/electricity/electricitycomplex";
		}else if(type.equals("week")){//周
			return "line/electricity/electricityweek";
		}else{
			return "line/electricity/electricitymonth";
		}
	}
	
	
	/**
	 * 整线综合能耗分析
	 * @param model
	 * @param type
	 * @return
	 */
	@RequestMapping("/comprehensive/{type}")
	public String comprehensiveAnalysis(Model model,@PathVariable(value = "type") String type){
		model.addAttribute("currentName","整线综合能耗分析");
		if(type.equals("day")){//日
			return "line/comprehensive/comprehensiveday";
		}else if(type.equals("complex")){//日综合
			return "line/comprehensive/comprehensivecomplex";
		}else if(type.equals("week")){//周
			return "line/comprehensive/comprehensiveweek";
		}else{
			return "line/comprehensive/comprehensivemonth";
		}
	}
	
	/**
	 * 整线平均功率分析
	 * @param model
	 * @param type
	 * @return
	 */
	@RequestMapping("/startrate/{type}")
	public String startrateAnalysis(Model model,@PathVariable(value = "type") String type){
		model.addAttribute("currentName","整线平均开动率分析");
		if(type.equals("day")){//日
			return "line/startrate/startrateday";
		}else if(type.equals("complex")){//日综合
			return "line/startrate/startratecomplex";
		}else if(type.equals("week")){//周
			return "line/startrate/startrateweek";
		}else{
			return "line/startrate/startratemonth";
		}
	}
	
	/**
	 * 整线电度损失分析
	 * @param model
	 * @return
	 */
	@RequestMapping("/loss")
	public String lossAnalysis(Model model){
		model.addAttribute("currentName","整线电度损失分析");
		return "line/loss/lossday";
	}
	
	
	/**
	 * 整线日分析
	 * @param type
	 * @param lineId
	 * @param belongDate
	 * @return
	 */
	@RequestMapping("/lineDay/{type}")
	public Map<String, Object> lineDay(@PathVariable(value = "type") String type,@RequestParam(value="lineId",required=false,defaultValue="0")int lineId,
			String belongDate){
		Map<String, Object> model=new HashMap<String, Object>();
		if(lineId==0){
			//获取第一条生产线
			LineConfig line=lineConfigService.findFirstLine();
			if(line!=null){
				if(type.equals("electricity")){
					lineService.getLineElectricityDayData(line.getId(), belongDate, model);
				}else if(type.equals("loss")){
					Equipment equ=equipmentService.listByLine(line.getId()).get(0);
					lineService.getLineElecricityLossData(equ.getId(), belongDate, model);
					
				}
			}
		}else{
			if(type.equals("electricity")){
				lineService.getLineElectricityDayData(lineId, belongDate, model);
			}
		}
		return model;
	}

	/**
	 * 整线日综合
	 * @param type
	 * @param lineId
	 * @param equId
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	@RequestMapping("/lineComplex/{type}")
	public Map<String, Object> lineComplex(@PathVariable(value = "type") String type,@RequestParam(value="lineId",required=false,defaultValue="0")int lineId,
			String startTime,String endTime){
		Map<String, Object> model=new HashMap<String, Object>();
		if(lineId==0){
			//获取第一条生产线
			LineConfig line=lineConfigService.findFirstLine();
			if(line!=null){
				if(type.equals("electricity")){
					lineService.getLineElectricityCompexData(line.getId(), startTime, endTime, model);
				}
			}
		}else{
			if(type.equals("electricity")){
				lineService.getLineElectricityCompexData(lineId, startTime, endTime, model);
			}
		}
		return model;
	}
	
	/**
	 * 整线电度电费周分析、月分析
	 * @param type
	 * @param lineId
	 * @param year
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	@RequestMapping("/lineElectricity/{type}")
	public Map<String, Object> lineElectricityMonthOrWeek(@PathVariable(value = "type") String type,@RequestParam(value="lineId",required=false,defaultValue="0")int lineId,
			int year,int startTime,int endTime){
		Map<String, Object> model=new HashMap<String, Object>();
		if(lineId==0){
			//获取第一条生产线
			LineConfig line=lineConfigService.findFirstLine();
			if(line!=null){
				if(type.equals("week")){
					lineService.getLineElecricityWeekData(line.getId(), year, startTime, endTime, model);
				}else{
					lineService.getLineElecricityMonthData(line.getId(), year, startTime, endTime, model);
				}
			}
		}else{
			if(type.equals("week")){
				lineService.getLineElecricityWeekData(lineId, year, startTime, endTime, model);
			}else{
				lineService.getLineElecricityMonthData(lineId, year, startTime, endTime, model);
			}
			
		}
		return model;
	}
	
	
	/**
	 * 月同比
	 * @param type
	 * @param lineId
	 * @param month
	 * @return
	 */
	@RequestMapping("/lineBySameMonth/{type}")
	public Map<String, Object> lineBySameMonth(@PathVariable(value = "type") String type,
			@RequestParam(value="lineId",required=false,defaultValue="0")int lineId,int month){
		Map<String, Object> model=new HashMap<String, Object>();
		if(lineId==0){
			//获取第一条生产线
			LineConfig line=lineConfigService.findFirstLine();
			if(line!=null){
				if(type.equals("electricityMonth")){
					lineService.getOnYOYBasisData(line.getId(), month, model);
				}
			}
		}else{
			if(type.equals("electricityMonth")){
				lineService.getOnYOYBasisData(lineId, month, model);
			}
		}
		return model;
	}

	/**
	 * 整线综合---日分析
	 * @param lineId
	 * @param equId
	 * @param belongDate
	 * @return
	 */
	@RequestMapping("/lineComprehensiveDay")
	public Map<String, Object> lineComprehensiveDay(@RequestParam(value="lineId",required=false,defaultValue="0")int lineId,
			@RequestParam(value="equId",required=false,defaultValue="0")int equId,String belongDate){
		Map<String, Object> model=new HashMap<String, Object>();
		if(lineId==0){
			//获取第一条生产线
			LineConfig line=lineConfigService.findFirstLine();
			if(equId==0){
				if(line!=null){
					//获取第一台设备
					Equipment equ=equipmentService.listByLine(line.getId()).get(0);
					lineService.getLineCompositeDayData(line.getId(), equ.getId(), belongDate, model);
				}
			}else{
				lineService.getLineCompositeDayData(line.getId(), equId, belongDate, model);
			}
		}else{
			if(equId==0){
				//获取该生产线下的第一个设备
				Equipment equ =equipmentService.listByLine(lineId).get(0);
				lineService.getLineCompositeDayData(lineId, equ.getId(), belongDate, model);
			}else{
				lineService.getLineCompositeDayData(lineId, equId, belongDate, model);
			}
		}
		return model;
	}
	
	/**
	 * 整线综合---日综合分析
	 * @param lineId
	 * @param equId
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	@RequestMapping("/lineComprehensiveComplex")
	public Map<String, Object> lineComprehensiveComplex(@RequestParam(value="lineId",required=false,defaultValue="0")int lineId,
			@RequestParam(value="equId",required=false,defaultValue="0")int equId,String startDate,String endDate){
		Map<String, Object> model=new HashMap<String, Object>();
		if(lineId==0){
			//获取第一条生产线
			LineConfig line=lineConfigService.findFirstLine();
			if(equId==0){
				if(line!=null){
					//获取第一台设备
					Equipment equ=equipmentService.listByLine(line.getId()).get(0);
					lineService.getLineCompositeCompexData(line.getId(), equ.getId(), startDate,endDate, model);
				}
			}else{
				lineService.getLineCompositeCompexData(line.getId(), equId, startDate,endDate, model);
			}
		}else{
			if(equId==0){
				//获取该生产线下的第一个设备
				Equipment equ =equipmentService.listByLine(lineId).get(0);
				lineService.getLineCompositeCompexData(lineId, equ.getId(), startDate, endDate,model);
			}else{
				lineService.getLineCompositeCompexData(lineId, equId, startDate,endDate, model);
			}
		}
		return model;
	}
	
	/**
	 * 整线综合---周分析
	 * @param lineId
	 * @param equId
	 * @param year
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	@RequestMapping("/lineComprehensiveWeek")
	public Map<String, Object> ComprehensiveWeek(@RequestParam(value="lineId",required=false,defaultValue="0")int lineId,
			@RequestParam(value="equId",required=false,defaultValue="0")int equId,int year,int startTime,int endTime){
		Map<String, Object> model=new HashMap<String, Object>();
		if(lineId==0){
			//获取第一条生产线
			LineConfig line=lineConfigService.findFirstLine();
			if(equId==0){
				if(line!=null){
					//获取第一台设备
					Equipment equ=equipmentService.listByLine(line.getId()).get(0);
					lineService.getLineCompositeWeekData(line.getId(), equ.getId(),year, startTime,endTime, model);
				}
			}else{
				lineService.getLineCompositeWeekData(line.getId(), equId,year, startTime,endTime, model);
			}
		}else{
			if(equId==0){
				//获取该生产线下的第一个设备
				Equipment equ =equipmentService.listByLine(lineId).get(0);
				lineService.getLineCompositeWeekData(lineId, equ.getId(),year, startTime,endTime, model);
			}else{
				lineService.getLineCompositeWeekData(lineId, equId,year, startTime,endTime, model);
			}
		}
		return model;
	}
	
	@RequestMapping("/lineComprehensiveMonth")
	public Map<String, Object> ComprehensiveMonth(@RequestParam(value="lineId",required=false,defaultValue="0")int lineId,
			@RequestParam(value="equId",required=false,defaultValue="0")int equId,int year,int startTime,int endTime){
		Map<String, Object> model=new HashMap<String, Object>();
		if(lineId==0){
			//获取第一条生产线
			LineConfig line=lineConfigService.findFirstLine();
			if(equId==0){
				if(line!=null){
					//获取第一台设备
					Equipment equ=equipmentService.listByLine(line.getId()).get(0);
					lineService.getLineCompositeMonthData(line.getId(), equ.getId(),year, startTime,endTime, model);
				}
			}else{
				lineService.getLineCompositeMonthData(line.getId(), equId,year, startTime,endTime, model);
			}
		}else{
			if(equId==0){
				//获取该生产线下的第一个设备
				Equipment equ =equipmentService.listByLine(lineId).get(0);
				lineService.getLineCompositeMonthData(lineId, equ.getId(),year, startTime,endTime, model);
			}else{
				lineService.getLineCompositeMonthData(lineId, equId,year, startTime,endTime, model);
			}
		}
		return model;
	}
	
	@RequestMapping("/lossElectricityData")
	public Map<String, Object> lossElectricityData(@RequestParam(value="lineId",required=false,defaultValue="0")int lineId,
			@RequestParam(value="equId",required=false,defaultValue="0")int equId,String belongDate){
		Map<String, Object> model=new HashMap<String, Object>();
		if(equId==0){
			//获取第一条生产线
			LineConfig line=lineConfigService.findFirstLine();
			if(line!=null){
				Equipment equ=equipmentService.listByLine(line.getId()).get(0);
				lineService.getLineElecricityLossData(equ.getId(), belongDate, model);
					
			}
		}else{
			lineService.getLineElecricityLossData(equId, belongDate, model);
		}
		return model;
	}
	
	@RequestMapping("/StartrateDayData")
	public Map<String, Object> StartrateDayData(@RequestParam(value="lineId",required=false,defaultValue="0")int lineId,
			@RequestParam(value="equId",required=false,defaultValue="0")int equId,String belongDate){
		Map<String, Object> model=new HashMap<String, Object>();
		if(lineId==0){
			//获取第一条生产线
			LineConfig line=lineConfigService.findFirstLine();
			if(line!=null){
				if(equId==0){
					Equipment equ=equipmentService.listByLine(line.getId()).get(0);
					lineService.getAverageStartRateDayData(line.getId(), equ.getId(), belongDate, model);
				}else{
					lineService.getAverageStartRateDayData(line.getId(), equId, belongDate, model);
				}
			}
		}else{
			if(equId==0){
				Equipment equ=equipmentService.listByLine(lineId).get(0);
				lineService.getAverageStartRateDayData(lineId, equ.getId(), belongDate, model);
			}else{
				lineService.getAverageStartRateDayData(lineId, equId, belongDate, model);
			}
		}
		return model;
	}
		
	@RequestMapping("/StartrateComplex")
	public Map<String, Object> StartrateComplex(@RequestParam(value="lineId",required=false,defaultValue="0")int lineId,
			@RequestParam(value="equId",required=false,defaultValue="0")int equId,String startDate,String endDate){
		Map<String, Object> model=new HashMap<String, Object>();
		if(lineId==0){
			//获取第一条生产线
			LineConfig line=lineConfigService.findFirstLine();
			if(equId==0){
				if(line!=null){
					//获取第一台设备
					Equipment equ=equipmentService.listByLine(line.getId()).get(0);
					lineService.getAverageStartRateCompositeData(line.getId(), equ.getId(), startDate,endDate, model);
				}
			}else{
				lineService.getAverageStartRateCompositeData(line.getId(), equId, startDate,endDate, model);
			}
		}else{
			if(equId==0){
				//获取该生产线下的第一个设备
				Equipment equ =equipmentService.listByLine(lineId).get(0);
				lineService.getAverageStartRateCompositeData(lineId, equ.getId(), startDate, endDate,model);
			}else{
				lineService.getAverageStartRateCompositeData(lineId, equId, startDate,endDate, model);
			}
		}
		return model;
	}
	
	@RequestMapping("/StartrateWeek")
	public Map<String, Object> StartrateWeek(@RequestParam(value="lineId",required=false,defaultValue="0")int lineId,
			@RequestParam(value="equId",required=false,defaultValue="0")int equId,int year,int startTime,int endTime){
		Map<String, Object> model=new HashMap<String, Object>();
		if(lineId==0){
			//获取第一条生产线
			LineConfig line=lineConfigService.findFirstLine();
			if(equId==0){
				if(line!=null){
					//获取第一台设备
					Equipment equ=equipmentService.listByLine(line.getId()).get(0);
					lineService.getAverageStartRateWeekData(line.getId(), equ.getId(),year, startTime,endTime, model);
				}
			}else{
				lineService.getAverageStartRateWeekData(line.getId(), equId,year, startTime,endTime, model);
			}
		}else{
			if(equId==0){
				//获取该生产线下的第一个设备
				Equipment equ =equipmentService.listByLine(lineId).get(0);
				lineService.getAverageStartRateWeekData(lineId, equ.getId(),year, startTime,endTime, model);
			}else{
				lineService.getAverageStartRateWeekData(lineId, equId,year, startTime,endTime, model);
			}
		}
		return model;
	}
	
	@RequestMapping("/StartrateMonth")
	public Map<String, Object> StartrateMonth(@RequestParam(value="lineId",required=false,defaultValue="0")int lineId,
			@RequestParam(value="equId",required=false,defaultValue="0")int equId,int year,int startTime,int endTime){
		Map<String, Object> model=new HashMap<String, Object>();
		if(lineId==0){
			//获取第一条生产线
			LineConfig line=lineConfigService.findFirstLine();
			if(equId==0){
				if(line!=null){
					//获取第一台设备
					Equipment equ=equipmentService.listByLine(line.getId()).get(0);
					lineService.getAverageStartRateMonthData(line.getId(), equ.getId(),year, startTime,endTime, model);
				}
			}else{
				lineService.getAverageStartRateMonthData(line.getId(), equId,year, startTime,endTime, model);
			}
		}else{
			if(equId==0){
				//获取该生产线下的第一个设备
				Equipment equ =equipmentService.listByLine(lineId).get(0);
				lineService.getAverageStartRateMonthData(lineId, equ.getId(),year, startTime,endTime, model);
			}else{
				lineService.getAverageStartRateMonthData(lineId, equId,year, startTime,endTime, model);
			}
		}
		return model;
	}
	
	/**
	 * 月度同比-------整线平均开动率----有待修改
	 * @param equId
	 * @param belongYear
	 * @param month
	 * @return
	 */
	@RequestMapping("/lineBySameMonth")
	public Map<String, Object> lineBySameMonth(int equId, int month){
		Map<String, Object> model=new HashMap<String, Object>();
		if(equId==0){
			//获取第一条生产线
			LineConfig line=lineConfigService.findFirstLine();
			Equipment equ=equipmentService.listByLine(line.getId()).get(0);
			lineService.getOnYOYStartRateData(equ.getId(),month, model);
		}else{
			lineService.getOnYOYStartRateData(equId,month, model);
		}
		return model;
	}
	/**
	 * 切换设备---周分析/月分析
	 * @param type
	 * @param equId
	 * @param year
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	@RequestMapping("/switchEquipment/{type}")
	public Map<String, Object> switchEquipmentByWeekOrMonth(@PathVariable(value = "type") String type,int equId,
			@RequestParam(value="belongDate",required=false,defaultValue="0")String belongDate,
			@RequestParam(value="startDate",required=false,defaultValue="0")String startDate,
			@RequestParam(value="endDate",required=false,defaultValue="0")String endDate,
			@RequestParam(value="year",required=false,defaultValue="0")int year,
			@RequestParam(value="startTime",required=false,defaultValue="0")int startTime,
			@RequestParam(value="endTime",required=false,defaultValue="0")int endTime){
		Map<String, Object> model=new HashMap<String, Object>();
		if(type.equals("day")){
			lineService.getMachineStartRateDayData(equId, belongDate, model);
		}else if(type.equals("complex")){
			lineService.getMachineStartRateCompexData(equId, startDate, endDate, model);
		}else if(type.equals("week")){
			lineService.getMachineStartRateWeekData(equId, year, startTime, endTime, model);
		}else{
			lineService.getMachineStartRateMonthData(equId, year, startTime, endTime, model);
		}
		return model;
	}
}
